﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using MaxTrader.Core;
using NHibernate;
using NHibernate.SqlTypes;

namespace MaxTrader.DAL.Mapping.CustomTypes
{
    internal sealed class CurrencyUserType: AbstractUserType<Currency, int>
    {
        public override object NullSafeGet(IDataReader rs, string[] names, object owner)
        {
            var code = (int)NHibernateUtil.Int32.NullSafeGet(rs, names[0]);
            return new Currency(code);
        }

        public override void NullSafeSet(IDbCommand cmd, object value, int index)
        {
            if (value == null)
            {
                NHibernateUtil.Int32.NullSafeSet(cmd, null, index);
            }
            else
            {
                var curr = (Currency)value;
                NHibernateUtil.Int32.NullSafeSet(cmd, curr.IsoNumericCode, index);
            }
        }

        public override SqlType[] SqlTypes
        {
            get { return new[] { SqlTypeFactory.Int32 }; }
        }

        public override Type ReturnedType
        {
            get { return typeof(int); }
        }
    }
}
